package topo.utils;

import com.zhtelecom.common.base.MyLog;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.util.ResourceBundle;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class DialogTools {
    static ResourceBundle res = ResourceBundle.getBundle(DialogTools.class.getPackage().getName() + ".Res");

    public static void setWindowPCCenter(Window frame) {
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height) {
            frameSize.height = screenSize.height;
        }
        if (frameSize.width > screenSize.width) {
            frameSize.width = screenSize.width;
        }
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
        frame.setVisible(true);
    }

    public static void dialogPack(Window jFrame, JDialog dlg) {
        dlg.setResizable(true);
        dlg.setModal(true);
        WindowPack(jFrame, dlg);
    }

    public static void WindowPack(Window jFrame, Window dlg) {
        if (jFrame == null) {
            setWindowPCCenter(dlg);
            return;
        }

        Dimension frmSize = jFrame.getSize();

        Dimension dlgSize = dlg.getSize();

        Point loc = jFrame.getLocation();

        dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);

        dlg.setVisible(true);
    }

    public static void showErroMsgFromEx(Component component, String msg, Exception ex) {
        JOptionPane.showMessageDialog(component, msg + res.getString("String_1") + ex.getMessage(), res.getString("String_2"), 0);
        MyLog.err(msg + res.getString("String_3") + ex);

        MyLog.debug(ex);
    }

    public static void showErroMsg(Component component, String msg) {
        JOptionPane.showMessageDialog(component, msg, res.getString("String_4"), 0);
    }

    public static void showOkMsg(Component component, String title, String msg) {
        JOptionPane.showMessageDialog(component, msg, title, 1);
    }

    public static boolean isOkConfim(Window window, String msg) {
        int iResult = JOptionPane.showConfirmDialog(window, msg, res.getString("String_5"), 0, 3);

        if (iResult == 0) {
            return true;
        }

        return false;
    }

    public static int isOkConfirm(Window window, String msg) {
        int iResult = JOptionPane.showConfirmDialog(window, msg, res.getString("String_5"), 1, 3);
        return iResult;
    }
}